import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
* Input format : M n1 n2 ( M : number of pair , n1, n2 are possible 0)
* n1 -> base (integer), n2 -> degree (integer)
* Can be sort or not.
* Output : #test case m1 m2
* If is 0, display 0 0
* If there is 0x => no display
* The degree is integer (may be negative)
* TODO: it is the best to use set / map
*/
public class SamsungTest3 {
	private int[] multyItem(final int[] first, final int[] second) {
		// Multiple (a,b) vs (c,d) => return (m, n) vs m : base, n : degree
		return new int[] { first[0] * second[0], first[1] + second[1] };
	}

	private int[][] multyArray(final int[][] first, final int[][] second) {
		// Processing to store result (base, degree)
		final int m = first.length;
		final int n = second.length;
		final int[][] result = new int[m * n][2];
		int result_index = 0;
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				int[] mulResult = multyItem(first[i], second[j]);
				result[result_index][0] = mulResult[0];
				result[result_index][1] = mulResult[1];
				result_index++;
			}
		}
		return result;
	}

	// The most expensive, o(n^2)
	private void groupArray(final int[][] data) {
		final int length = data.length;
		for (int i = 0; i < length - 1; i++) {
			for (int j = i + 1; j < length; j++) {
				// If same degree => sum base
				if (data[i][1] == data[j][1]) {
					data[i][0] += data[j][0];
					// Reset the value
					data[j][1] = data[j][0] = 0;
				}
			}
		}
	}

	// Sort
	private void sortArray(final int[][] data) {
		final int length = data.length;
		for (int i = 0; i < length; i++) {
			int temp = data[i][1];
			for (int j = 0; j < length; j++) {
				if (data[j][1] < temp) {
					int[] temp_object = data[i];
					data[i] = data[j];
					data[j] = temp_object;
				}
			}
		}
	}

	public static void main(String[] args) throws FileNotFoundException {
		final int NUM_TEST = 1;

		SamsungTest3 test = new SamsungTest3();
		System.setIn(new FileInputStream("data\\samsung3.txt"));
		Scanner scanner = new Scanner(System.in);
		final long start = System.currentTimeMillis();
		for (int index = 0; index < NUM_TEST; index++) {
			// Get the input
			final int m = scanner.nextInt();
			final int[][] first = new int[m][2];
			for (int i = 0; i < m; i++) {
				first[i][0] = scanner.nextInt();
				first[i][1] = scanner.nextInt();
			}
			final int n = scanner.nextInt();
			final int[][] second = new int[n][2];
			for (int i = 0; i < n; i++) {
				second[i][0] = scanner.nextInt();
				second[i][1] = scanner.nextInt();
			}
			// Get result
			final int[][] mul_result = test.multyArray(first, second);
			// Group it all
			test.groupArray(mul_result);
			// Sort
			test.sortArray(mul_result);
			// Print
			int result_length = mul_result.length;
			for (int i = 0; i < result_length; i++) {
				if (result_length == 1 || mul_result[i][0] != 0) {
					System.out.print(mul_result[i][0] + " " + mul_result[i][1]
							+ " ");
				}
			}
		}
		System.out.println("End after : "
				+ (System.currentTimeMillis() - start));
		scanner.close();
	}
}
